package com.hongye.why.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.hongye.why.R;
import com.hongye.why.app.UpdateManager;
import com.hongye.why.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * activity基类
 */
public abstract class BaseActivity extends AppCompatActivity implements LifeSubscriber {
    /**
     * 标题
     */
    protected TitleView titleView;
    /**
     * 内容
     */
    private FrameLayout contentLayout;
    /**
     * 所有依附我的生命
     */
    private final List<Life> lifes = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityStack.push(this);
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_root_layout);
        titleView = (TitleView) findViewById(R.id.root_title);
        contentLayout = (FrameLayout) findViewById(R.id.root_content);
        getLayoutInflater().inflate(inflateLayout(),contentLayout,true);
        setTitle(getTitle());
        onCreate();

        if (UpdateManager.updateManager().isNeedUpdate()) {
            UpdateManager.updateManager().showUpdateDialog(this);
        }
    }

    public void showTitle(boolean isShow) {
        if (titleView == null) return;
        if (isShow) {
            titleView.setVisibility(View.VISIBLE);
        } else {
            titleView.setVisibility(View.GONE);
        }
    }
    /**
     * 导入内容布局
     * @return 布局资源id
     */
    public abstract int inflateLayout();

    /**
     * 创建
     */
    public abstract void onCreate();

    public void setFragment(BaseFragment fragment) {
        setFragment(fragment, false);
    }

    public void setFragment(BaseFragment fragment,boolean isToBackStack) {
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.root_content, fragment);
        if (isToBackStack) {
            transaction.addToBackStack(fragment.toString());
        } else {
            transaction.disallowAddToBackStack();
        }
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        for (Life life : lifes) {
            if (life != null) {
                life.lifeEnd();
            }
        }
        lifes.clear();
        ActivityStack.remove(this);
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (titleView != null && !TextUtils.isEmpty(title)) {
            titleView.setTitle(title.toString());
        }
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (titleView != null) {
            titleView.setTitle(titleId);
        }
    }

    public TitleView getTitleView() {
        return titleView;
    }

    @Override
    public void attach(Life life) {
        if (!lifes.contains(life)) {
            lifes.add(life);
        }
    }

    @Override
    public void detach(Life life) {
        lifes.remove(life);
    }

    public final void showBack() {
        if (titleView != null) {
            titleView.addLeftDrawableFinish(this);
        }
    }


    /**
     * 关闭软键盘
     */
    public void closeHideSoftInput() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
